package ltd.hyangyang.vegetable.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.hyangyang.vegetable.entity.ClassificationEntity;
import ltd.hyangyang.vegetable.service.ClassificationService;
import ltd.hyangyang.vegetable.utils.MinIOProperties;
import ltd.hyangyang.vegetable.utils.MinIOUtils;
import ltd.hyangyang.vegetable.utils.MultipartFileToFile;
import ltd.hyangyang.vegetable.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RestController
@RequestMapping("/classification")
public class ClassificationController {
    @Autowired
    private ClassificationService classificationService;

    @Autowired
    MinIOProperties minIOProperties;

    @ResponseBody
    @GetMapping("/distinguish")
    public Result<ClassificationEntity> distinguish(String fileName){
        String dirPath = "C:\\Users\\hyy\\Pictures\\training";
        ClassificationEntity classificationEntity =classificationService.distinguish(dirPath,fileName);
        System.out.println(JSON.toJSONString(classificationEntity));
        return Result.success(classificationEntity);
    }

    @GetMapping("/getDistinguishClassId")
    public Result<Integer> getDistinguishId(String fileName){
        String dirPath = "C:\\Users\\hyy\\Pictures\\training";
        Integer classId =classificationService.getDistinguishId(dirPath,fileName);

        return Result.success(classId);
    }

    @RequestMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file,@RequestParam("fileName")String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        String dirPath = "C:\\Users\\hyy\\Pictures\\training";
//
        MultipartFileToFile.saveMultipartFile(file, dirPath,fileName);
        String src = MinIOUtils.fileUpload(minIOProperties, file);
        return Result.success(src);
    }

    //查询所有的分类
    @RequestMapping("/getByCondition")
    public Result<Page<ClassificationEntity>> getByCondition(ClassificationEntity classification){
        Page<ClassificationEntity> classificationEntityPage = classificationService.getAll(classification);
        //获得
        return Result.success(classificationEntityPage);
    }

    //根据关键词查询分类
    @RequestMapping("/searchByKeyWord")
    public Result<List<ClassificationEntity>> searchByKeyWord(String keyword){
        List<ClassificationEntity> classificationEntityList = classificationService.search(keyword);
        return Result.success(classificationEntityList);
    }

    //根据class_id 查询分类
    @GetMapping("/getClassificationById")
    public Result<ClassificationEntity> getCondition(Integer classId){
        if (classId==null){
            return null;
        }
        ClassificationEntity classification = classificationService.getById(classId);
        return Result.success(classification);
    }

    //修改分类
    @RequestMapping("/editClassification")
    public Result updateClassification(@RequestBody ClassificationEntity classificationEntity){
        //传递的参数中classificationId不能为空
        classificationService.updateById(classificationEntity);
        return Result.success();
    }

    //根据序号查询数据
    @RequestMapping("/getInfoById/{id}")
    public Result<ClassificationEntity> getInfoById(@PathVariable Integer id){
        ClassificationEntity classificationEntity = classificationService.getById(id);
        return Result.success(classificationEntity);
    }

    @RequestMapping("getAll")
    public Result<List<ClassificationEntity>> getAll(){
        List<ClassificationEntity> classificationEntities = classificationService.getAllList();
        return Result.success(classificationEntities);
    }

    //查询所有父分类，并且构成三级分类
    @RequestMapping("/categoryTree")
    public Result<List<ClassificationEntity>> categoryTree(){
        List<ClassificationEntity> classificationEntities = classificationService.categoryTree();
        System.out.println(JSON.toJSONString(classificationEntities));
        return Result.success(classificationEntities);
    }
}
